package com.qiaotongtianxia.loans.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.ToastUtil;
import com.qiaotongtianxia.loans.views.BaseEditText;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.ButtonObserver;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/11/18.
 *
 * @author lisen < 4533548588@qq.com >
 */

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.tv_nav_title)
    BaseTextView tvNavTitle;
    @BindView(R.id.et_account)
    BaseEditText etAccount;
    @BindView(R.id.et_nickName)
    BaseEditText etNickName;
    @BindView(R.id.et_password)
    BaseEditText etPassword;
    @BindView(R.id.et_confirmPw)
    BaseEditText etConfirmPw;
    @BindView(R.id.btn_register)
    ButtonObserver btnRegister;
    @BindView(R.id.et_verify)
    BaseEditText etVerify;
    @BindView(R.id.tv_verifyCode)
    BaseTextView tvVerifyCode;
    @BindView(R.id.et_recommend)
    BaseEditText etRecommend;
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long millisUntilFinished) {
            tvVerifyCode.setEnabled(false);
            tvVerifyCode.setTextColor(ContextCompat.getColor(RegisterActivity.this,R.color.gray));
            tvVerifyCode.setText(millisUntilFinished / 1000 +"s");
        }
        @Override
        public void onFinish() {
            tvVerifyCode.setTextColor(ContextCompat.getColor(RegisterActivity.this,R.color.mainColor));
            tvVerifyCode.setText("获取验证码");
            tvVerifyCode.setEnabled(true);
        }
    };
    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        btnRegister.observers(etAccount, etNickName, etPassword, etConfirmPw,etVerify);
    }

    @Override
    public Api onApiCreate() {
        return new Api(this);
    }

    @Override
    public void initTitle() {
        tvNavTitle.setText(getString(R.string.register));
    }

    @OnClick({R.id.iv_nav_back,R.id.tv_verifyCode, R.id.btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_back:
                finish();
                break;
            case R.id.tv_verifyCode:
                String phone = etAccount.getText().toString();
                if (TextUtils.isEmpty(phone)){
                    ToastUtil.showShort(this,"请先输入手机号");
                    return;
                }
                api.verifyCode(phone, new IBaseRequestImp<String>() {
                    @Override
                    public void onRequestSuccess(String s) {
                        timer.start();
                    }
                });
                break;
            case R.id.btn_register://注册
                String account = etAccount.getText().toString();
                String nickName = etNickName.getText().toString();
                String password = etPassword.getText().toString();
                String confimPw = etConfirmPw.getText().toString();
                String sms = etVerify.getText().toString();
                String recommend = etRecommend.getText().toString();
                if (password.contains(" ")) {
                    ToastUtil.showShort(this, "密码不能包含空格");
                    return;
                }
                if (password.length() < 6 || password.length() > 16) {
                    ToastUtil.showShort(this, getString(R.string.pleaseInput) + getString(R.string.passwordHint));
                    return;
                }
                if (!password.equals(confimPw)) {
                    ToastUtil.showShort(this, getString(R.string.confirmPwError));
                    return;
                }
                api.registe(account, password, confimPw, nickName,sms,recommend,
                        new IBaseRequestImp<String>() {
                    @Override
                    public void onRequestSuccess(String s) {
                        ToastUtil.showShort(RegisterActivity.this, s);
                        finish();
                    }
                });
                break;
        }
    }
    @Override
    protected void onDestroy() {
        if (timer != null){
            timer.cancel();
        }
        super.onDestroy();
    }
}
